package com.hjlh.aimedical.service.impl;

import com.hjlh.aimedical.common.respone.ResultModel;
import com.hjlh.aimedical.entity.Medicalrecord;
import com.hjlh.aimedical.dao.MedicalrecordDao;
import com.hjlh.aimedical.service.MedicalrecordService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * (Medicalrecord)表服务实现类
 *
 * @author makejava
 * @since 2025-06-29 14:44:33
 */
@Service("medicalrecordService")
public class MedicalrecordServiceImpl implements MedicalrecordService {

    @Resource
    private MedicalrecordDao medicalrecordDao;

    private static final Logger logger = LoggerFactory.getLogger(MedicalrecordServiceImpl.class);

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Medicalrecord queryById(Integer id) {
        return this.medicalrecordDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @return 查询结果
     */
    @Override
    public Map<String, Object> queryByPage(int pageSize, int page, String usercode) {
        Map<String, Object> map = new HashMap<>();
        Medicalrecord medicalrecord = new Medicalrecord();
        medicalrecord.setUsercode(usercode);
        long total = this.medicalrecordDao.count(medicalrecord);
        PageRequest pageRequest = PageRequest.of(page-1,pageSize);
        List<Medicalrecord> list = this.medicalrecordDao.queryAllByLimit(medicalrecord, pageRequest);

        map.put("total", total);
        map.put("rows", list);
        return map;
    }




    /**
     * 新增数据
     *
     * @param medicalrecord 实例对象
     * @return 实例对象
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultModel insert(Medicalrecord medicalrecord) {
        try {
            Date time = new Date();
            medicalrecord.setCode(UUID.randomUUID().toString());
            medicalrecord.setCreatedtime(time);
            medicalrecord.setName(medicalrecord.getZhushu() + time.getTime());
            this.medicalrecordDao.insert(medicalrecord);
            return ResultModel.success("创建成功",null);
        }catch (Exception e){
            logger.error("insert fail",e);
        }
        return ResultModel.fail("创建失败",null);
    }

    /**
     * 修改数据
     *
     * @param medicalrecord 实例对象
     * @return 实例对象
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultModel update(Medicalrecord medicalrecord) {
        try {
            Medicalrecord m = medicalrecordDao.queryByCode(medicalrecord.getCode());
            if(m == null ){
                //重新创建
                Date time = new Date();
                medicalrecord.setCode(UUID.randomUUID().toString());
                medicalrecord.setCreatedtime(time);
                medicalrecord.setName(medicalrecord.getZhushu() + time.getTime());

                this.medicalrecordDao.insert(medicalrecord);
                return ResultModel.success("创建成功",null);
            }
            m.setMedicalrecord(medicalrecord.getMedicalrecord());
            this.medicalrecordDao.update(m);
            return ResultModel.success("创建成功",null);
        }catch (Exception e){
            logger.error("创建失败",e);
        }
        return ResultModel.fail("创建失败",null);
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.medicalrecordDao.deleteById(id) > 0;
    }
}
